package com.hlf.modules.merchant.merchant.service.impl;

import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.PageUtils;
import com.hlf.modules.merchant.merchant.entity.Merchant;
import com.hlf.modules.merchant.merchant.mapper.MerchantMapper;
import com.hlf.modules.merchant.merchant.service.MerchantService;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/14.
 */
@Service("merchantService")
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Merchant queryObject(Long id) {
        return merchantMapper.queryObject(id);
    }

    @Override
    public List<Merchant> queryList(Map<String, Object> map) {
        return merchantMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return merchantMapper.queryTotal(map);
    }

    @Override
    public void save(Merchant merchant) {
        merchantMapper.insertSelective(merchant);
    }

    @Override
    public void update(Merchant merchant) {
        merchantMapper.update(merchant);
    }

    @Override
    public void delete(Long id) {
        merchantMapper.delete(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        merchantMapper.deleteBatch(ids);
    }

    @Override
    public PageUtils queryListForPage(Map<String, Object> map) {
        return null;
    }

    /**
     * 切换门店
     *
     * @param
     * @param merchantId
     * @param storeId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStore(Long merchantId, String storeId) {

        Merchant merchant = queryObject(merchantId);
        Long userId = merchant.getUserId();
        SysUserEntity sysUserEntity = sysUserMapper.queryObject(userId);
        sysUserEntity.setCurrentStoreId(String.valueOf(storeId));
        int i = sysUserMapper.updateByPrimaryKeySelective(sysUserEntity);
        if (i == 0) {
            throw new BusinessException("切换失败!");
        }

    }

    /**
     * 根据用户id查商家
     * @param userId
     * @return
     */
    @Override
    public Merchant queryForUserId(Long userId) {
        return merchantMapper.queryObjectForUserId(userId);
    }
}
